/*
  ID: nemokin1
  PROG: comehome
  LANG: C++
*/
#include <vector>
#include <list>
#include <assert.h>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <fstream>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <fstream>
 
using namespace std;
 
#define REP(i,n) for(int i=0; i<(n); ++i)
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define FORD(i,a,b) for(int i=(a);i>=b;--i)
#define FOREACH(it,c) for(typeof((c).begin()) it=(c).begin();it!=(c).end();++it)
#define ALL(c) (c).begin(),(c).end()
 
#define debug(x) cerr<< #x << " = " << (x) << "\n";
#define debugv(v) cerr << #v << " = "; FOREACH(it,v) cerr << *it <<","; cerr << "\n";
#define SIZE(a)  (int(a.size()))
 
template<typename T> inline void checkMax(T& a, T b) {if (b>a) a=b;}
template<typename T> inline void checkMin(T& a, T b) {if (b<a) a=b;}
 
typedef long long LL;
typedef vector<int> VI;
typedef vector<string> VS;
typedef pair<int,int>  ii;
typedef long double ld;
const int INF = 1000000000;

int dist[200][200];

int main() {
    //freopen("comehome.in","r",stdin);freopen("comehome.out","w",stdout);
    int N;
    cin >> N;
    map<char,int> m;
    map<int,char> nn;
    
    int cnt = 0;
    int qt = N;
    REP(i,200) REP(j,200) dist[i][j] = 987654321;
    
    REP(i,N){
	char a,b;
	int c;
	cin >> a >> b >> c;
	if(m.count(a) == 0){if(a == 'Z') qt = cnt;m[a] = cnt;nn[cnt] = a; cnt++;}
	if(m.count(b) == 0){if(b == 'Z') qt = cnt;m[b] = cnt;nn[cnt] = b; cnt++;}
	if(dist[m[a]][m[b]]>c) dist[m[a]][m[b]] = dist[m[b]][m[a]] = c;
    }

    priority_queue<ii,vector<ii>,greater<ii> > q;
    q.push(ii(0,qt));
    VI D(cnt,987654321);
    D[qt] = 0;
    
    while(!q.empty()){
	ii top = q.top();
	q.pop();
	int n = top.second, w = top.first;
	//cout << nn[n] << " " << w << endl;
	//if(w <= D[n]){
	    for(int i = 0; i < cnt; i++){
		if(dist[n][i] == 987654321) continue;
		if(D[i] > D[n]+dist[n][i]){
		    D[i] = D[n] + dist[n][i];
		    q.push(ii(D[i],i));
		}
	    }
        //}
    }
    int ret = 987654321;
    char rea;
    
    for(int i = 0; i < cnt; i++){
	//cout << nn[i] << " " <<D[i] << endl;
	
	if(nn[i] == 'Z') continue;
	if(isupper(nn[i])){
	    if(D[i] < ret){
		ret = D[i];
		rea = nn[i];
	    }
	}
    }

    cout << rea << " " << ret << endl;
}

		
    

    

